{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.7.4-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python37432bit8d8e1828c3004b5281cefc61c8c7a538",
   "display_name": "Python 3.7.4 32-bit"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "array = np.array([[1,2,3],\n",
    "                  [4,5,6],\n",
    "                  [7,8,9]])\n",
    "print(array)\n",
    "print(array.ndim)#维度\n",
    "print(array.shape)#形状\n",
    "print(array.size)#大小\n",
    "print(array.dtype)#元素类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([1,2,3],dtype=np.int32)\n",
    "zero = np.zeros((2,3)) #生成2行3列全为0的矩阵\n",
    "one = np.ones((3,4)) #生成3行4列全为1的矩阵\n",
    "empty = np.empty((3,2))#生成3行2列全都接近于0（不等于0）的矩阵\n",
    "e = np.arange(10) # [0 1 2 3 4 5 6 7 8 9]\n",
    "g = np.arange(1,20,3) # [ 1  4  7 10 13 16 19]\n",
    "h = np.arange(8).reshape(4,2)#重新定义矩阵的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([[1,2,3], [4,5,6]])\n",
    "arr2 = np.array([[1,1,2], [2,3,3]])\n",
    "arr3 = np.ones((3,5))\n",
    "print(arr1 + arr2)\n",
    "print(arr1 - arr2)\n",
    "print(arr1 * arr2)\n",
    "print(arr1 // arr2)\n",
    "print(arr1 ** arr2)\n",
    "print(arr1 % arr2)\n",
    "print(arr1+2) #所有的元素加2\n",
    "print(arr1 > 3) #判断哪些元素大于3\n",
    "print(np.dot(arr1,arr4))\n",
    "print(arr1.dot(arr4))#矩阵乘法\n",
    "print(arr1.T)#矩阵转置\n",
    "print(np.transpose(arr1))#矩阵转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample1 = np.random.random((3,2))#生成3行2列从0到1的随机数\n",
    "sample2 = np.random.normal(size=(3,2))#生成3行2列符合标准正态\n",
    "sample3 = np.random.randint(0,10,size=(3,2))#生成3行2列从0到\n",
    "np.sum(sample1)#求和\n",
    "np.min(sample1)#求最小值\n",
    "np.max(sample1)#求最大值\n",
    "np.sum(sample1,axis=0)#对列求和\n",
    "np.sum(sample1,axis=1)#对行求和\n",
    "np.argmin(sample1)#求最小值的索引\n",
    "np.argmax(sample1)#求最大值的索引\n",
    "print(np.mean(sample1))#求平均值\n",
    "print(sample1.mean())#求平均值\n",
    "np.median(sample1)#求中位数\n",
    "np.sqrt(sample1)#开方\n",
    "np.sort(sample1)#排序\n",
    "np.clip(sample4,2,7)#小于2就变成2，大于7就变为7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.arange(2,14)\n",
    "print(arr1[2])#第二个位置的数据\n",
    "print(arr1[1:4])#第一到第四个位置的数据\n",
    "print(arr1[2:-1])#第二到倒数第一个位置的数据\n",
    "print(arr1[:5])#前五个数据\n",
    "print(arr1[-2:])#最后两个数据\n",
    "arr2 = arr1.reshape(3,4)\n",
    "print(arr2[1][1])\n",
    "print(arr2[:,2])\n",
    "for i in arr2: #迭代行\n",
    "    print(i)\n",
    "for i in arr2.flat:#一个一个元素迭代\n",
    "    print(i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([1,2,3])\n",
    "arr2 = np.array([4,5,6])\n",
    "arr3 = np.vstack((arr1,arr2))#垂直合并\n",
    "arr4 = np.hstack((arr1,arr2))#水平合并\n",
    "arrv = np.vstack((arr1,arr2,arr3))\n",
    "arr = np.concatenate((arr1,arr2,arr1)) # 连接\n",
    "arr = np.concatenate((arr3,arrv),axis=0)#合并的array维度要相同，array形状要匹配，axis=0纵向合并\n",
    "print(arr1[np.newaxis,:])\n",
    "print(arr1[:,np.newaxis])\n",
    "print(np.atleast_2d(arr1))# 变成至少二维"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.arange(12).reshape((3,4))\n",
    "arr2,arr3 = np.split(arr1,2,axis=1)#水平方向分割，分成2份\n",
    "arr7,arr8,arr9 = np.array_split(arr1,3,axis=1)#水平方向分割，\n",
    "arrv1,arrv2,arrv3 = np.vsplit(arr1,3)#垂直分割\n",
    "arrh1,arrh2 = np.hsplit(arr1,2)#水平分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([1,2,3])\n",
    "arr2 = arr1#arr1,arr2共享一块内存，浅拷贝\n",
    "arr3 = arr1.copy()#深拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "numpy.linspace(start, stop[, num=50[, endpoint=True[, retstep=False[, dtype=None]]]]])\n",
    "\n",
    "# 返回在指定范围内的均匀间隔的数字（组成的数组），也即返回一个等差数列"
   ]
  }
 ]
}